import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:fate_blind_box/network/http_request.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../../utils/common_widget.dart';
import '../../../../utils/map/map_location_view.dart';
import '../../../main/model/shop_matchmaker_list_model.dart';
import '../canteen.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';

class NearByMapPage extends StatefulWidget {
  List<Marker>? pointMarkList = [];
  Function? locationBack;
  String? titleStr;

  NearByMapPage({
    Key? key,
    this.pointMarkList,
    this.locationBack,
    this.titleStr,
  }) : super(key: key);

  @override
  State<NearByMapPage> createState() => _NearByMapPageState();
}

class _NearByMapPageState extends State<NearByMapPage> {
  TextStyle fontStyle =
      TextStyle(fontSize: 14, color: ThemeColor.hint777777Color);
  NearByMapController nearByMapController = Get.put(NearByMapController());
  List dataList = [];

  @override
  Widget build(BuildContext context) {
    return AppWidget.navTitleBar(
        title: widget.titleStr ?? '单身驿站',
        childWidget:Column(
          children: [
            Expanded(
              child: MapLocationView(
                marks: widget.pointMarkList,
                locationBlock: (lati, longi, res) {
                  getShopListData(longi, lati);
                },
              ),
            ),
            bottomSheet(context),
          ],
        ));
  }

  bottomSheet(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 17, top: 10),
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(16), topRight: Radius.circular(16))),
      height: 1.sh / 2.0,
      child: GetBuilder(
          id: nearByMapController.listViewId,
          builder: (NearByMapController controller) {
            return (dataList.isEmpty)
                ? Container()
                : ListView.builder(
                    itemCount: dataList.length,
                    itemBuilder: (BuildContext itemContext, int index) {
                      return shopItemView(dataList[index]);
                    });
          }),
    );
  }

  Widget shopItemView(ShopMatchMakerListDataModel model) {
    return GestureDetector(
      onTap: (){
        CustomShareView().showShareView(Get.context!,
            dataModel: model,
            locationPoint: LatLng(
              model.latitude,
              model.longitude,
            ),
            saddress: model.address);
      },
      child: Container(
        color: Colors.white,
        margin: const EdgeInsets.only(bottom: 14),
        child: Row(
          children: [
            AppWidget.commonEmptyImg(
                url: model.matchPhoto ?? '',
                imgWidth: 66,
                imgHeight: 66,
                radius: 5),
            const SizedBox(
              width: 10,
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ConstrainedBox(
                  constraints: BoxConstraints(maxWidth: 1.sw - 17 - 66 - 10),
                  child: Text(
                    (model.shortName ?? ''),
                    maxLines: 1,
                    style: TextStyle(
                        fontSize: 18,
                        color: ThemeColor.textColor3,
                        overflow: TextOverflow.ellipsis),
                  ),
                ),
                const SizedBox(
                  height: 8,
                ),
                Row(
                  children: [
                    Text(
                      (model.area ?? '') + ' | ',
                      style: fontStyle,
                    ),
                    Text(
                      model.distance ??'',
                      style: fontStyle,
                    ),
                  ],
                ),
                const SizedBox(
                  height: 12,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    getShopListData(0.000000, 0.000000);
  }

  getShopListData(longitude, latitude) async {
    var res = await HttpRequest.request('match-maker/business/nearby',
        params: {'nearbyLon': longitude, 'nearbyLat': latitude});
    if(dataList.isNotEmpty){
      dataList.clear();
    }
    if ((res is Map) && res['data'] != null) {
      List resList = res['data'];
      for (var element in resList) {
        ShopMatchMakerListDataModel nearByListModel =
        ShopMatchMakerListDataModel.fromJson(element);
        if (nearByListModel.shortName != null &&
            nearByListModel.shortName!.isNotEmpty) {
          dataList.add(nearByListModel);
        }
      }
      nearByMapController.update([nearByMapController.listViewId]);
    }
  }
}

class NearByMapController extends GetxController {
  String listViewId = 'listViewId';
}
